using System;
using System.Collections;
using System.Xml;
using FormsGenerator.Xml;
using FormsGeneratorPlugins.IO;
using FormsGeneratorPlugins.Form.IO;
using System.Collections.Generic;

namespace FormsGenerator
{
    public partial class PrintFormData : System.Web.UI.Page
    {
        private string _guid;
        private string _resultId;
            
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["id"]))
            {
                LabelErrorMessage.Text = "id is empty. Please provide a form id!";
                LabelErrorMessage.Visible = true;
            }
            else
            {
                _guid = Request["id"].Trim();
                _resultId = Request["resultid"].Trim();
                if (!IsPostBack)
                {
                    FormsGeneratorRenderForm1.DisplaySubmitButton = false;
                    FormsGeneratorRenderForm1.AllowNavigationEvenIfPageIsInvalid = true;
                    FormsGeneratorRenderForm1.UploadedFileHttpHandlerUrl = "GetFile.ashx";
                    
                    if (!string.IsNullOrEmpty(_resultId) && !string.IsNullOrEmpty(_guid))
                    {
                        int resultId = int.Parse(_resultId);
                        FormResultsDBHandler dataHandler = new FormResultsDBHandler();
                        Hashtable result = dataHandler.GetResult(resultId);
                        Dictionary<string, List<UploadedFile>> uploadedFiles = dataHandler.GetUploadedFiles(resultId, false);
                        XmlDocument formXmlDocument = XmlHandler.GetForm(_guid);
                        FormsGeneratorRenderForm1.UploadedFiles = uploadedFiles;
                        FormsGeneratorRenderForm1.FormXml = formXmlDocument.OuterXml;
                        FormsGeneratorRenderForm1.FormData = result;
                        FormsGeneratorRenderForm1.DisplayAllPages = true;
                        FormsGeneratorRenderForm1.TableCellSpacing = 0;
                        FormsGeneratorRenderForm1.RenderMode = FormsGeneratorPlugins.WebControls.FormRenderMode.Print;
                        FormsGeneratorRenderForm1.DataBind();

                    }
                }
            }
        }
    }
}

